package com.kz.demo;

import com.kz.framework.annotation.MyAutowired;
import com.kz.framework.annotation.MyController;
import com.kz.framework.annotation.MyRequestMapping;
import com.kz.framework.entity.View;
import com.kz.framework.enums.RequestMethod;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author zhgp
 * @version 1.0
 * @description: TODO
 * @date 2021/6/20 12:11
 */

@MyController
public class TestController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);

    ThreadLocal<Integer> iii = new ThreadLocal<Integer>();

    @MyAutowired
    private TestService testService;

    /**
     *
     * @return
     */
    @MyRequestMapping(value = "/userList", method = RequestMethod.GET)
    public View getUserList() {
        LOGGER.info("执行控制器方法...............");
        List<User> userList = testService.getAllUser();
        return new View("index.jsp").addModel("userList", userList);
    }

    public void test() {
        System.out.println("执行目标方法");
    }

    public static void main(final String[] arg) throws Exception {
        Class cls = Class.forName("com.kz.demo.TestController");
        Method method = cls.getDeclaredMethod("getUserList");
        Object proxyController = Enhancer.create(TestController.class, new MethodInterceptor() {

            public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams,
                MethodProxy methodProxy) throws Throwable {
                System.out.println("55555555555555555555555");
                Object o = methodProxy.invokeSuper(targetObject, methodParams);
                System.out.println("66666666666666666666666");
                return o;
            }
        });

        TestController c = (TestController)proxyController;
        c.test();

    }
}
